// pages/packet/packet.js
const app = getApp();
var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');
var util = require('../../utils/util');

const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  redpacketnum:'',
	  userInfo:null,
	  itemDtl:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  this.setData({
	    	userInfo: app.globalData.userInfo,
	    	redpacketnum: options.redpacketnum
	    });
	  this.getRedBagDetail();
  },
  onReady:function(){

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getRedBagDetail:function(){
	  var that=this;
	  var method = route.getRedBagDetail();
	  var params = {
			  userId: app.globalData.userId,
			  redPacketNum:that.data.redpacketnum
	  };
	  httpClient.request(method, 'GET', params, function (res, err) {
		  var records = res.records;
		  for(var i=0;i<records.length;i++){
			  records[i].receiveDate = util.formatTime(new Date(records[i].receiveDate));
		  }
		  that.setData({
			  itemDtl : res
		  });
	  }); 
  },
  withdraw: function (event) {
    wx.switchTab({
      url: '../widthdraw/widthdraw'
    })
  },
  send: function (event) {
    wx.switchTab({
      url: '../index/index'
    })
  },
  turn: function (event) {
    wx.navigateTo({
      url: '../creatpacket/creatpacket'
    })
  },
  //开始录音的时候
  start: function () {
	  const options = {
      duration: 10000,//指定录音的时长，单位 ms
      sampleRate: 16000,//采样率
      numberOfChannels: 1,//录音通道数
      encodeBitRate: 96000,//编码码率
      format: 'mp3',//音频格式，有效值 aac/mp3
      frameSize: 50,//指定帧大小，单位 KB
    }
    //开始录音
    recorderManager.start(options);
    recorderManager.onStart(() => {
      console.log('recorder start')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
  //停止录音
  stop: function () {
    recorderManager.stop();
    recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      console.log('停止录音', res.tempFilePath);
      this.upload(res.tempFilePath);
      
    })
  },
  upload:function(filePath){
      setTimeout(function () {
          var url=config.host_url + route.getReceive();
          console.log(url)
          var formData={userId:3,redPacketNum:"991e3bef5e069713"};
          wx.uploadFile({
              url: url,
              filePath: filePath,
              name: 'file',
              formData:formData,
              header: {
                  'content-type': 'multipart/form-data'
              },
              success: function (res) {
            	  console.log(res);
              },
              fail: function (res) {
                  console.log(res);
                  wx.showModal({
                      title: '提示',
                      content: "网络请求失败，请确保网络是否正常",
                      showCancel: false,
                      success: function (res) {
       
                      }
                  });
              }
          });
          },1000);
  },audioPlay:function(e){
	  debugger
	 var src=e.currentTarget.dataset.src;
	  /*   this.setData({
		  src:src
	  });
	  this.audioCtx.play();*/
//	  innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
	  innerAudioContext.src = src;
	  innerAudioContext.play();//兼容起见用它

	  console.log(innerAudioContext);
      innerAudioContext.onPlay(() => {
        console.log("播放");
      })
	  innerAudioContext.onError((res) => {
	      console.log(res.errMsg)
	      console.log(res.errCode)
	  })

  }
  
})